//
//  LoginViewController.m
//  DaMaoMall
//
//  Created by HXHL-wally on 16/7/19.
//  Copyright © 2016年 daMao-wally. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetViewController.h"
#import "LoginViewModel.h"

@interface LoginViewController ()

@property (strong, nonatomic) UIImageView * dmImageView;

@property (strong, nonatomic) UIImageView * phoneIV;

@property (strong, nonatomic) UIImageView * passWordIV;

@property (strong, nonatomic)  UITextField *nameField;

@property (strong, nonatomic)  UITextField *passWordField;

@property (strong, nonatomic) UIButton * eyeBTN;

@property (strong, nonatomic)  UIButton *registerBTN;

@property (strong, nonatomic)  UIButton *forgetPasswordBTN;

@property (strong, nonatomic)  UIButton *loginBTN;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.contentView.backgroundColor = UIColorFromRGB(0xffffff);
    self.view.backgroundColor = UIColorFromRGB(0xffffff);
    [self setUpPresentBTN];
    [self setUpAssembleView];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationBarView.hidden = YES;
    self.navigationBarBg.backgroundColor = UIColorFromRGB(0xffffff);
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.navigationBarView.hidden = YES;
    self.navigationBarBg.backgroundColor = UIColorFromRGB(0xffffff);
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

#pragma mark - 创建取消模态视图按钮
- (void)setUpPresentBTN{
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(20, 32, 10, 20)];
    [btn setBackgroundImage:[UIImage imageNamed:@"Login-icon-back"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(nav_Back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
#pragma mark - 取消模态出来的视图
- (void)nav_Back{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}



#pragma mark - 创建视图
- (void)setUpAssembleView
{
    //logo
    UIImageView * dmImageView = [UIImageView new];
    self.dmImageView = dmImageView;
    [self.contentView addSubview:self.dmImageView];
    self.dmImageView.sd_layout.widthIs(GetScreenWidth*0.504).heightIs(GetScreenHeight*0.07).topSpaceToView(self.contentView,GetScreenHeight*0.11).leftSpaceToView(self.contentView,GetScreenWidth*0.248);
    self.dmImageView.image = [UIImage imageNamed:@"damao.cn"];
    
    //手机号
    UIImageView * phoneIV = [UIImageView new];
    self.phoneIV = phoneIV;
    [self.contentView addSubview:self.phoneIV];
    phoneIV.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.dmImageView,GetScreenHeight*0.11).leftSpaceToView(self.contentView,46);
    phoneIV.image = [UIImage imageNamed:@"bg-mm"];
    
    
    UILabel * nameLB = [UILabel new];
    [self.contentView addSubview:nameLB];
    nameLB.sd_layout.widthIs(50).heightIs(20).topSpaceToView(self.dmImageView,GetScreenHeight*0.124).leftSpaceToView(self.contentView,GetScreenWidth*0.16);
    nameLB.text = @"手机号:";
    nameLB.textColor = UIColorFromRGB(0x808080);
    nameLB.font = [UIFont systemFontOfSize:15];
    
    
    UITextField * nameTF = [UITextField new];
    self.nameField = nameTF;
    [self.contentView addSubview:self.nameField];
    self.nameField.sd_layout.widthIs(GetScreenWidth*0.5).heightIs(20).topSpaceToView(self.dmImageView,GetScreenHeight*0.124).leftSpaceToView(self.contentView,GetScreenWidth*0.33);
    self.nameField.font = [UIFont systemFontOfSize:15];
    self.nameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.nameField.placeholder = @"请输入您的手机号";
    self.nameField.keyboardType = UIKeyboardTypeNumberPad;
    self.nameField.textColor = UIColorFromRGB(0xb9b9b9);
    
    //密码
    UIImageView * passWordIV = [UIImageView new];
    self.passWordIV = passWordIV;
    [self.contentView addSubview:self.passWordIV];
    passWordIV.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.phoneIV,GetScreenHeight*0.028).leftSpaceToView(self.contentView,46);
    passWordIV.image = [UIImage imageNamed:@"bg-mm"];
    
    
    UILabel * passWordLB = [UILabel new];
    [self.contentView addSubview:passWordLB];
    passWordLB.sd_layout.widthIs(55).heightIs(20).topSpaceToView(self.phoneIV,GetScreenHeight*0.0415).leftSpaceToView(self.contentView,GetScreenWidth*0.16);
    passWordLB.text = @"密    码:";
    passWordLB.textColor = UIColorFromRGB(0x808080);
    passWordLB.font = [UIFont systemFontOfSize:15];
    
    
    UITextField * passWordTF = [UITextField new];
    self.passWordField = passWordTF;
    [self.contentView addSubview:self.passWordField];
    passWordTF.sd_layout.widthIs(GetScreenWidth*0.43).heightIs(20).topSpaceToView(self.phoneIV,GetScreenHeight*0.0415).leftSpaceToView(self.contentView,GetScreenWidth*0.33);
    self.passWordField.font = [UIFont systemFontOfSize:15];
    self.passWordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passWordField.placeholder = @"请输入您的密码";
    self.passWordField.secureTextEntry = YES;
    self.passWordField.delegate = self;
    self.passWordField.textColor = UIColorFromRGB(0xb9b9b9);
    
    // 眼睛
    UIButton * eyeBTN = [UIButton new];
    self.eyeBTN = eyeBTN;
    [self.contentView addSubview:self.eyeBTN];
    self.eyeBTN.sd_layout.widthIs(GetScreenWidth*0.1066).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.phoneIV,GetScreenHeight*0.028).leftSpaceToView(self.contentView,GetScreenWidth*0.755);
    [self.eyeBTN setImage:[UIImage imageNamed:@"icon-eye1"] forState:UIControlStateNormal];
    [self.eyeBTN addTarget:self action:@selector(clickEyeBTN:) forControlEvents:UIControlEventTouchUpInside];
    
    //登录
    UIButton * loginBTN = [UIButton new];
    self.loginBTN = loginBTN;
    [self.contentView addSubview:self.loginBTN];
    self.loginBTN.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.passWordIV,GetScreenHeight*0.0615).leftSpaceToView(self.contentView,46);
    [self.loginBTN setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginBTN setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginBTN setBackgroundImage:[UIImage imageNamed:@"btn-dl"] forState:UIControlStateNormal];
    [self.loginBTN addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    //注册
    UIButton *  registerBTN = [UIButton new];
    self.registerBTN = registerBTN;
    [self.contentView addSubview:self.registerBTN];
    self.registerBTN.sd_layout.widthIs(60).heightIs(40).topSpaceToView(self.contentView,GetScreenHeight*0.8).leftSpaceToView(self.contentView,30);
    [self.registerBTN setTitle:@"注册" forState:UIControlStateNormal];
    [self.registerBTN setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
    self.registerBTN.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.registerBTN addTarget:self action:@selector(signUp) forControlEvents:UIControlEventTouchUpInside];
    
    //忘记密码
    UIButton * forgetPassWordBTN = [UIButton new];
    self.forgetPasswordBTN = forgetPassWordBTN;
    [self.contentView addSubview:self.forgetPasswordBTN];
    self.forgetPasswordBTN.sd_layout.widthIs(60).heightIs(40).topSpaceToView(self.contentView,GetScreenHeight*0.8).leftSpaceToView(self.contentView,GetScreenWidth*0.74);
    [self.forgetPasswordBTN setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.forgetPasswordBTN setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
    self.forgetPasswordBTN.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.forgetPasswordBTN addTarget:self action:@selector(forgetPassWord) forControlEvents:UIControlEventTouchUpInside];
}

- (void)signUp{
    
    RegisterViewController * signUp = [[RegisterViewController alloc]init];
    signUp.navigationItem.hidesBackButton = YES;
    [self.navigationController pushViewController:signUp animated:YES];
}

- (void)forgetPassWord{
    
    ForgetViewController * forget = [[ForgetViewController alloc]init];
   
    forget.navigationItem.hidesBackButton = YES;
    [self.navigationController pushViewController:forget animated:YES];
    
}
- (void)login{

    [self.passWordField resignFirstResponder];
    [self.nameField resignFirstResponder];
    
        if (![DMTools chickIphoneNumberRight:self.nameField.text]) {//正确格式的手机号
            [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号"];
        }else if(self.passWordField.text.length < 6){
            [SVProgressHUD showInfoWithStatus:@"密码长度至少6位"];
        }else {
            NSDictionary *jsonDict = @{@"username":self.nameField.text,
                                       @"password":self.passWordField.text,
                                       @"create_time":[SecurityUtil timeStamp]};
            [LoginViewModel oprationClient:jsonDict type:@"1" success:^{
                //登录成功进入首页或者个人信息页面 TO DO..
                
            }];
        }
}


- (void)clickEyeBTN:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    // 改变button 的  状态图片
    
    if(btn.selected){
        //改变 密码 的 显示为明文（默认密文）
        self.passWordField.secureTextEntry = NO;
        [btn setImage:[UIImage imageNamed:@"icon-eye2"] forState:UIControlStateNormal];
        
    }else{
        //改变 密码 的 显示为密文
        self.passWordField.secureTextEntry = YES;
        [btn setImage:[UIImage imageNamed:@"icon-eye1"] forState:UIControlStateNormal];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//点击return 按钮 去掉
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.passWordField resignFirstResponder];
    return YES;
}
//点击屏幕空白处去掉键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nameField resignFirstResponder];
    [self.passWordField resignFirstResponder];
}

@end
